//
//  FFRedPacketsModule.m
//  LiMaoRedPackets
//
//  Created by JayLuo on 2020/8/20.
//

#import "FFRedPacketsModule.h"
#import "FFSendRedPacketsVC.h"
#import "FFRedPacketsRecordVC.h"
#import "IQKeyboardManager.h"
#import "FFOpenRedPacketVC.h"
#import "FFRewardConfig.h"
#import "FFRedPacketsContent.h"
#import "FFRedPacketsCell.h"
#import "FFWallItemListCell.h"
#import "FFRedPacketsAllDetailRecordVC.h"
#import "FFWalletVC.h"
#import "FFRedPacketsSystemContent.h"
#import "FFRedPacketsSystemCell.h"
#import "FFSendRedPacketsVC.h"
#import "FFPanelRedpacketFuncItem.h"
@WKModule(FFRedPacketsModule)

@implementation FFRedPacketsModule
+ (NSString *) gmoduleId {
    return @"LiMaoRedPackets";
}
- (NSString *) moduleId {
    return [FFRedPacketsModule gmoduleId];
}

- (void)moduleInit:(WKModuleContext *)context {
    NSLog(@"【LiMaoRedPackets】模块初始化！");
    
    // 注册消息
    // 红包消息
    [[WKApp shared] registerCellClass:FFRedPacketsCell.class forMessageContntClass:FFRedPacketsContent.class];
    // 红包系统消息
    [[WKApp shared] registerCellClass:FFRedPacketsSystemCell.class forMessageContntClass:FFRedPacketsSystemContent.class];
    
    // 更多面板 -> 红包
    __weak typeof(self) weakSelf = self;
    [[WKApp shared] setMethod:@"send.redpacket" handler:^id _Nullable(id  _Nonnull param) {
        return [WKMoreItemModel initWithImage:[weakSelf imageName:@"more_redpacket"] title:LLang(@"发红包") onClick:^(id<WKConversationContext>  context) {
            
            // 判断群和单聊 1单聊 2群聊
            int type = context.channel.channelType;
            // 跳转
            FFSendRedPacketsVC *vc = [[FFSendRedPacketsVC alloc] init];
            vc.channel = context.channel;
            vc.redPacketsType = type;
            if (type == 2) {
              
                vc.membersCount =  [[WKSDK shared].channelManager getMemberCount:context.channel];
            }
            vc.sendPacketComplete = ^(NSDictionary  * _Nonnull dict) {
                [weakSelf sendRedPackets:context dict:dict];
            };
            [[WKNavigationManager shared] pushViewController:vc animated:YES];
            
        }];
    } category:WKPOINT_CATEGORY_PANELMORE_ITEMS sort:3];
    
    
    [[WKApp shared] setMethod:@"goWallet" handler:^id _Nullable(id  _Nonnull param) {
        [[WKNavigationManager shared] pushViewController:[FFWalletVC new] animated:YES];
        return nil;
    }];
    
    // 红包记录(依赖于钱包模块)
    [[WKApp shared] setMethod:@"wallet.item.redpacket" handler:^id _Nullable(id  _Nonnull param) {
        return [FFWallItemModel initWithTitle:LLang(@"红包记录") icon:[self imageName:@"icon_redpacket"] onClick:^{
            
            // 跳转红包记录
            FFRedPacketsAllDetailRecordVC *vc = [[FFRedPacketsAllDetailRecordVC alloc] init];
            [[WKNavigationManager shared] pushViewController:vc animated:YES];
        }];
    } category:@"wallet.item" sort:-1];
    
    // 红包
    [[WKApp shared] setMethod:@"panel.func.item.redpacket" handler:^id _Nullable(id  _Nonnull param) {
        FFPanelRedpacketFuncItem *item = [[FFPanelRedpacketFuncItem alloc] init];
        item.sort = 9000;
        return item;
    } category:WKPOINT_CATEGORY_PANELFUNCITEM];
  
    
}

- (BOOL)moduleDidFinishLaunching:(WKModuleContext *)context {
    IQKeyboardManager.sharedManager.enable = NO;
    [[IQKeyboardManager sharedManager].enabledDistanceHandlingClasses addObject:FFSendRedPacketsVC.class];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    return YES;
}

// 发红包
- (void)sendRedPackets:(id<WKConversationContext>)context dict:(NSDictionary *)dict {
    __weak typeof(context) weakContext = context;
    
    NSString *redPacketNo = dict[@"redpacket_no"];
    NSString *blessing = dict[@"blessing"];
    [weakContext sendMessage:[FFRedPacketsContent redPacketNo:redPacketNo blessing:blessing]];
}

- (UIImage*) imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LiMaoRedPackets"];
}
@end
